const config = require('config.js')
const db = require('db.js')

function sign (data,callback) {
  wx.showLoading({
    title: '注册中...',
    mask: true
  })
  wx.request({
    url: config.serverHost + '/api/sign',
    data: data,
    header: {
      'content-type': 'application/json'
    },
    method: 'POST',
    success(res) {
      wx.hideLoading()
      callback(res)
    }
  })
}

function login (data,callback) { 
  wx.showLoading({
    title: '登录中...',
    mask: true
  })
  wx.request({
    url: config.serverHost + '/api/login',
    data: data,
    header: {
      'content-type': 'application/json'
    },
    method: 'POST',
    success (res) {
      wx.hideLoading()
      callback(res)
    },
    fail: function () {
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请检查网络状态!',
      })
    }
  })
}

function getData (url, data, callback) {
  wx.showLoading({
    title: '加载中...',
    mask: true
  })
  wx.request({
    url: config.serverHost + '/api/'+url,
    header: {
      'content-type': 'application/json'
    },
    data: data,
    method: 'GET',
    success(res) {
      wx.hideLoading()
     callback(res)
    },
    fail: function () {
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请检查网络状态!',
      })
    }
  })
}

function post(url, data, callback) {
  wx.showLoading({
    title: '加载中...',
    mask: true
  })
  wx.request({
    url: config.serverHost + '/api/' + url,
    data: data,
    header: {
      'content-type': 'application/json'
    },
    method: 'POST',
    success(res) {
      wx.hideLoading()
      callback(res)
    },
    fail: function () {
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请检查网络状态!',
      })
    }
  })
}

function puts(url, data, callback) {
  wx.showLoading({
    title: '加载中...',
    mask: true
  })
  wx.request({
    url: config.serverHost + '/api/' + url,
    data: data,
    header: {
      'content-type': 'application/json'
    },
    method: 'PUT',
    success(res) {
      wx.hideLoading()
      callback(res)
    },
    fail: function () {
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请检查网络状态!',
      })
    }
  })
}

function deleteData(url, data, callback) {
  wx.showLoading({
    title: '加载中...',
    mask: true
  })
  wx.request({
    url: config.serverHost + '/api/' + url,
    data: data,
    header: {
      'content-type': 'application/json'
    },
    method: 'DELETE',
    success(res) {
      wx.hideLoading()
      callback(res)
    },
    fail: function () {
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请检查网络状态!',
      })
    }
  })
}

function updateData(url, data, callback) {
  wx.showLoading({
    title: '加载中...',
    mask: true
  })
  wx.request({
    url: config.serverHost + '/api/' + url,
    data: data,
    header: {
      'content-type': 'application/json'
    },
    method: 'PUT',
    success(res) {
      wx.hideLoading()
      callback(res)
    },
    fail: function () {
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请检查网络状态!',
      })
    }
  })
}

module.exports = {
  sign: sign,
  login: login,
  getData: getData,
  post: post,
  puts: puts,
  deleteData: deleteData,
  updateData: updateData
}